<?php
/**
 * ArticlesMenu is a widget displaying article's categories
 */
class ArticlesMenu extends CWidget
{

	public function run()
	{
		$menu = $this->saveDataAsHtml(Category::listing());
		$this->render('articlesMenu',array('menu'=>$menu));
	}

	private function saveDataAsHtml($data)
	{
		$html='';
		if(is_array($data))
		{
			foreach($data as $node)
			{
				if(!isset($node['text']))
					continue;
				$id=isset($node['id']) ? (' id="'.$node['id'].'"') : '';
				if(isset($node['expanded']))
					$css=$node['expanded'] ? 'open' : 'closed';
				else
					$css='';
				if(isset($node['hasChildren']) && $node['hasChildren'])
				{
					if($css!=='')
						$css.=' ';
					$css.='hasChildren';
				}
				if($css!=='')
					$css=' class="'.$css.'"';
				
				if(!empty($node['children']))
				{
					$html.="<li{$id}{$css}>{$node['text']}{$node['texte']}";
					$html.="\n<ul>\n";
					$html.=self::saveDataAsHtml($node['children']);
					$html.="</ul>\n";
				}else{
					$html.="<li{$id}{$css}>{$node['text']}{$node['click']}{$node['texte']}";
				}
				$html.="</li>\n";
			}
		}
		return $html;
	}
}
?>

